from obspy import read
import pandas as pd
import json
import datetime
import time
import numpy as np



path = './Deci5.Pick.19991015130000.CI.CDY.EHZ.sac'
st = read(path)
print(st)
print(st[0].stats)
print(st[0].data)
print(st[0].stats.starttime)
print(st[0].stats.delta)
delta = st[0].stats.delta * 1496101
print(st[0].stats.starttime + delta)
print(st[0].stats.endtime)
import matplotlib.pyplot as plt

'''
开始时间以每个点0.05秒增加，即20hz
时间格式是1999-10-16T09:46:43.997000Z
下面以2017-10-13T00:00:00.000000Z为开始时间，1000hz频率转化时间戳
'''
path2 = './jsonfile.json'
f = open(path2,'r')
dict_json = json.load(f)
print(dict_json)

cegun_path = dict_json['cegun'][0]
print(cegun_path)

df = pd.read_csv(cegun_path,header=None)
print(df)
print(df[1])
time_col = df[1]
# df.plot(x=1,y=2)
# plt.show()

# unix时间以秒为单位
# a = 1507824000.0
# temp_time = time.localtime(a)
# struct_time = time.strftime("%Y-%m-%dT%H:%M:%S.%fZ", temp_time)
# print(struct_time)

# 计算差值
# dif_time = a - 7427.692
# print(7427.692+dif_time)
# print(7427.693+dif_time)
# temp_time = time.localtime(7427.693+dif_time)
# struct_time = temp_time
# print(struct_time)

# print(str(st[0].stats.starttime))
# starttime = datetime.datetime.strptime(str(st[0].stats.starttime), '%Y-%m-%dT%H:%M:%S.%fZ')
# print(starttime)

# a_datetime = datetime.datetime.strptime("2016-11-15, 15:32:12, 2", "%Y-%m-%d, %H:%M:%S, %w")
# print(a_datetime)
# a_datetime_local = datetime.datetime.now()  # 获取datetime.datetime类型的本地时间
# print(a_datetime_local)
# a_datetime_utc = datetime.datetime.utcnow()  # 获取datetime.datetime类型的utc时间
# print(a_datetime_utc)
#
# time_stamp = a_datetime_local.timestamp()  # datetime类型转时间戳
# print(time_stamp)
#
# a = 1507824000
# utc_time = datetime.datetime.fromtimestamp(a)
# print(utc_time)
# aa = datetime.timedelta(seconds=3600*8)
# print(utc_time+aa)
# utc_time = utc_time+aa
# time_stamp = utc_time.timestamp()
# print(time_stamp)

# a = 1507824000
# s = float(time_col[0])
# dif = a-s
# new_df = time_col+dif
# print(new_df)
# dif = round(a - s,4)
# print(time_col[0])
# print(dif)
# num = 7427.693
# print(num)
# nums = 7427.693 + 1507816572.308
# print(nums)
# # new_df = (time_col+dif)
# # print(new_df[2])
# print(datetime.datetime.utcfromtimestamp(nums))
print(df[1])
s = 1507824000000
data_list = []
for i in range(len(time_col)):
    ss = datetime.datetime.utcfromtimestamp(s/1000)
    sss = ss.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
    data_list.append(sss)
    s+=1
    i+=1
print(len(data_list))
# print(data_list[0])
np_data_list = np.array(data_list)
df[1] = np_data_list
print(df)
out_path = './cegun2017_10_12.csv'
df.to_csv(out_path,header=0)
